// js/utils/ScreenAdapter.js
export default class ScreenAdapter {
  constructor() {
    const systemInfo = wx.getWindowInfo();
    this.screenWidth = systemInfo.windowWidth;
    this.screenHeight = systemInfo.windowHeight;
    this.pixelRatio = systemInfo.pixelRatio

    // 设计尺寸
    this.DESIGN_WIDTH = 1624;
    this.DESIGN_HEIGHT = 750;
    // this.DESIGN_WIDTH = 750;
    // this.DESIGN_HEIGHT = 1624;

    // 计算缩放和偏移
    this.scaleX = this.screenWidth / this.DESIGN_WIDTH;
    this.scaleY = this.screenHeight / this.DESIGN_HEIGHT;
    this.scale = Math.min(this.scaleX, this.scaleY);

    this.offsetX = (this.screenWidth - this.DESIGN_WIDTH * this.scale) / 2;
    this.offsetY = (this.screenHeight - this.DESIGN_HEIGHT * this.scale) / 2;

    // 设置Canvas的物理像素大小，使其等于屏幕大小，保证清晰度
    this.canvasWidth = this.screenWidth * this.pixelRatio;
    this.canvasHeight = this.screenHeight * this.pixelRatio;

    console.log('ScreenAdapter初始化:', {
      屏幕尺寸: `${this.screenWidth}x${this.screenHeight}`,
      设备像素比: this.pixelRatio,
      设计尺寸: `${this.DESIGN_WIDTH}x${this.DESIGN_HEIGHT}`,
      缩放比例: this.scale,
      画布尺寸: `${this.canvasWidth}x${this.canvasHeight}`
    });
  }

  // **【新增】核心转换函数**
  // 将屏幕坐标转换为游戏逻辑坐标
  screenToLogic(screenX, screenY) {
    // 1. 减去偏移量，得到相对于游戏内容区域的坐标
    const relativeX = screenX - this.offsetX;
    const relativeY = screenY - this.offsetY;

    // 2. 除以缩放比，得到逻辑坐标
    const logicX = relativeX / this.scale;
    const logicY = relativeY / this.scale;

    return { x: logicX, y: logicY };
  }
}
